"""
request 请求
response 响应
url 请求地址
get 发起get请求 用于获取数据
post 发起post请求 用于提交数据
"""

import requests
import re
import time


# print(requests)
# print(dir(requests))

# def get_title(url):
#     # url = "https://www.baidu.com"
#     response = requests.get(url=url)
#     # print(response, dir(response))
#     # print(type(response.content))
#     content = response.content.decode("UTF8")
#     # print(content, type(content))
#     title = re.search(r'<title.*?>(.*?)</title>', content)
#     if title:
#         return title.group(1)
#
# print(get_title("https://www.baidu.com"))
# print(get_title("https://www.jd.com"))
# print(get_title("https://www.pinduoduo.com"))


# def save_image(url):
#     response = requests.get(url)
#     content = response.content
#     with open("./image/pinduoduo.jpg", "wb") as f:
#         f.write(content)
#
# save_image("https://cdn.pinduoduo.com/upload/2020-12-14/7531b9a5-e242-4124-89df-254df0862ab5.jpg")


# def save_school():
#     url = "https://tieba.baidu.com/t/f/?class=college"
#     response = requests.get(url)
#     # print(response.content.decode("utf8"))
#     # print(response.text)
#     schools = re.findall(r'<a class="each_topic_entrance_item" href="//tieba.baidu.com/t/f/.*?" data-fid=".*?">(.*?)</a>', response.text)
#     with open("./myfolder/schools.txt", "w", encoding="utf8") as f:
#         for school in schools:
#             f.write(school.replace(" ", ""))
#             f.write("\n")
#
# save_school()


def save_hot():
    for i in range(1, 18):
        print(f"正在处理第{i}页")
        url = f"https://tieba.baidu.com/f/index/rcmdForum?pn={i}&rn=12"
        resopnse = requests.get(url)
        datas = resopnse.json()['data']['forum_info']
        # print(datas, type(datas))

        for data in datas:
            name = data['forum_name']
            avatar_url = data['avatar']
            # print("正在处理", f"{name}.jpg", avatar_url)
            avatar_response = requests.get(avatar_url)
            with open(f"./hot/{name}.jpg", "wb") as f:
                f.write(avatar_response.content)
        time.sleep(1)


save_hot()
